<?php
function headerValidation()
{
    //开发模式 取消验证
    if (env('APP_DEBUG')) return false;
    $header = [];
    $timeliness = '1800000';//过期时间
    $spe_key = config('app.spe_key');
    $header['time'] = request()->header('time');
    $header['uid'] = request()->header('uid');
    $header['sign'] = request()->header('sign');
    //验证头信息
    foreach ($header as $key => $value) {
        if(empty($value)){
            return '[签名验证]输入的信息不完整...';
        }
    }
    $time = time();
    //时效性验证
    if($time-$header['time']>$timeliness){
        return '[签名验证]签名超时...';
    }

    //验证签名
    $sign = md5($header['uid'].$header['time'].$spe_key);
    if($sign!=$header['sign']){
        return '[签名验证]失败...';
    }
    return false;
}


/**
 * curl模拟GET/POST发送请求
 * @param string $url 请求的链接
 * @param array $data 请求的参数
 * @param string $timeout 请求超时时间
 * @return mixed
 * @since 1.0.1
 */
function curl($url, $data = array(), $timeout = 5) {
    $ch = curl_init();
    if (!empty($data) && $data) {
        if(is_array($data)){
            $formdata = http_build_query($data);
        } else {
            $formdata = $data;
        }
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $formdata);
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_ENCODING, '');
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

function getIp(){
    if (! empty($_SERVER['HTTP_CLIENT_IP'])) {
        $cip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $cip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0];
    } elseif (! empty($_SERVER['REMOTE_ADDR'])) {
        $cip = $_SERVER['REMOTE_ADDR'];
    } else {
        $cip = '';
    }
    return $cip;
}
